package com.newsproject.controller;

import com.newsproject.pojo.Result;
import com.newsproject.pojo.vo.CommentVo;
import com.newsproject.service.PostService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Api(tags = "评论模块")
@Controller
public class CommentController {
    @Autowired
    private PostService postService;
    @ApiOperation(value = "查询新闻的评论及回复评论")
    @ResponseBody
    @GetMapping("/NewsController/selectCommentTree")
    public Result<Map<String, Object>> selectComment(@ApiParam("新闻的id") @RequestParam int postId)
    {
        return postService.getCommentsWithReplies(postId);
    }
    @ApiOperation(value = "普通评论",notes = "传递评论内容和新闻的id")
    @PostMapping("/NewsController/comment")
    @ResponseBody
    public Result<Map<String,Object>> comment(@ApiParam("评论内容")@RequestParam String content,
                                              @ApiParam("文章id")@RequestParam Integer postId,
                                              HttpSession session
    ) throws Exception {
        return postService.comment(content,postId,session);
    }
    @ApiOperation(value = "回复评论", notes = "content评论内容,parentId父类评论的id")
    @PostMapping("/NewsController/insertComment")
    @ResponseBody
    public Result<Object> insertComment(@ApiParam("回复的内容")@RequestParam String content,@ApiParam("回复的评论的id")@RequestParam int parentId, @ApiIgnore HttpSession session)
    {
        return postService.insertComment(content,parentId,session);
    }
    @ApiOperation(value = "删除评论及评论回复", notes = "需要评论Id和新闻id,用于删除评论并改变post中储存的count_comment的值")
    @GetMapping("/NewsController/deleteComment")
    @ResponseBody
    public Result<Object> deleteComment(@RequestParam int commentId, @ApiIgnore HttpSession session)
    {
        return postService.deleteComment(commentId,session);
    }
    @ApiOperation(value = "判断用户是否点赞评论，评论是否有回复")
    @ResponseBody
    @GetMapping("/NewsController/commentstatus")
    public Result<Map<String,Object>> commentStatus(@ApiParam("评论ID")@RequestParam int commentId,@ApiIgnore HttpSession session)
    {
        return postService.commentstatus(commentId,session);
    }
    @ApiOperation(value = "获取评论",notes = "传递新闻的id")
    @GetMapping("/NewsController/selectComment/{postId}")
    @ResponseBody
    public Result<List<CommentVo>> selectCommentById(@PathVariable Integer postId) {
        return  postService.getAllComment(postId);
    }
    @ApiOperation("作者：根据id删除评论或者回复（不会判断是不是自己发的）")
    @PostMapping ("/NewsController/deleteComment/{commentId}")
    @ResponseBody
    public String deleteCommentById(@PathVariable("commentId") Integer commentId) {
        postService.deleteComment(commentId);
        return "删除成功";
    }

    @ApiOperation("根据id删除评论或者是回复（进行判断：是不是自己发布的评论或者是回复）")
    @DeleteMapping ("/NewsController/delete/{commentId}")
    @ResponseBody
    public Result<Map<String, Object>> deleteById(@PathVariable("commentId") Integer commentId, @ApiIgnore HttpSession session) {
        return postService.deleteAndJudge(commentId,session);
    }

}
